﻿using System;

namespace Hx.Infrastructure.Exceptions
{
    public class ErrorCodeException : Exception
    {
        private int _errorCode;

        /// <summary>
        ///     创建带错误代码的异常
        /// </summary>
        /// <param name="errorCodePrefix">错误代码前缀</param>
        /// <param name="errorCode">错误代码</param>
        public ErrorCodeException(int errorCodePrefix, int errorCode)
            : this(errorCodePrefix, errorCode, null, null)
        {
        }

        /// <summary>
        ///     创建带错误代码的异常
        /// </summary>
        /// <param name="errorCodePrefix">错误代码前缀</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMsg">错误信息</param>
        public ErrorCodeException(int errorCodePrefix, int errorCode, string errorMsg)
            : this(errorCodePrefix, errorCode, errorMsg, null)
        {
            
        }

        /// <summary>
        ///     创建带错误代码的异常
        /// </summary>
        /// <param name="errorCodePrefix">错误代码前缀</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMsg">错误信息</param>
        /// <param name="innerException">内部异常</param>
        public ErrorCodeException(int errorCodePrefix, int errorCode, string errorMsg, Exception innerException)
            : base(errorMsg, innerException)
        {
            ErrorCodePrefix = errorCodePrefix;
            _errorCode = ErrorCodePrefix + errorCode;
        }

        /// <summary>
        ///     错误代码前缀
        /// </summary>
        public int ErrorCodePrefix { get; set; }

        /// <summary>
        ///     错误代码
        /// </summary>
        public int ErrorCode
        {
            get { return _errorCode; }
            set { _errorCode = ErrorCodePrefix + value; }
        }
    }
}